package com.example.wssapp;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MediaActivity extends Activity implements View.OnClickListener {
	
	// 拍照request code
	private static final int CAPTURE_REQUEST_CODE= 100;
	
	// 多媒体加载本地文件
	private MediaPlayer localMediaPlayer;
	private Button localPrepareButton;
	private Button localPlayButton;
	
	// 多媒体加载远程文件
	private MediaPlayer remoteMediaPlayer;
	private Button remotePlayButton;
	private Button remotePrepareButton;
	
	// 拍照demo
	private Button takePhotoButton;
	private ImageView takedPhotoIv;
	
	// 加载远程图片
	private ImageView remoteImage;
	private Handler handler;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_media);
		
		localPlayButton = (Button)findViewById(R.id.local_play_button);
		localPrepareButton = (Button)findViewById(R.id.local_prepare_button);
		localPlayButton.setOnClickListener(this);
		localPrepareButton.setOnClickListener(this);
		
		remotePrepareButton = (Button)findViewById(R.id.remote_prepare_button);
		remotePlayButton = (Button)findViewById(R.id.remote_play_button);
		remotePrepareButton.setOnClickListener(this);
		remotePlayButton.setOnClickListener(this);
		
		takePhotoButton = (Button)findViewById(R.id.take_photo_button);
		takedPhotoIv = (ImageView)findViewById(R.id.taked_picture);
		takePhotoButton.setOnClickListener(this);
		
		remoteImage = (ImageView)findViewById(R.id.remote_image);
		handler = new Handler(this.getMainLooper()){
			
			public void handleMessage(android.os.Message msg) {
				super.handleMessage(msg);
				Bundle data = msg.getData();
				byte[] content = data.getByteArray("data");
				Bitmap bm = BitmapFactory.decodeByteArray(content, 0, content.length);
				remoteImage.setImageBitmap(bm);
			};
		};
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					URL url = new URL("http://assets0.tianmaying.com/img/logo/logo.png");
					HttpURLConnection connection = (HttpURLConnection)url.openConnection();
					connection.setConnectTimeout(0);
					connection.setDoInput(true);
					 connection.connect();
					
					InputStream is = connection.getInputStream();
					
					byte[] content = new byte[is.available()];
					is.read(content);
					
					Bundle bundle = new Bundle();
					bundle.putByteArray("data", content);
					Message msg = new Message();
					msg.setData(bundle);
					handler.sendMessage(msg);
				} catch (Exception e) {
				}
			}
		}).start();
	}
	
	private void initLocalMediaPlayer() {
		localMediaPlayer = MediaPlayer.create(this, R.raw.nan_shan_nan);
		try {
			if(null != localMediaPlayer) {
				localMediaPlayer.stop();
			}
			localMediaPlayer.prepare();
		} catch (Exception e) {
			Toast.makeText(this, "加载本地音乐文件异常："+ e.getMessage(), Toast.LENGTH_SHORT).show();
		} 
	}
	
	private void initRemoteMediaPlayer() {
		remoteMediaPlayer = MediaPlayer.create(this, Uri.parse("http://192.168.1.106:8080/media/nan_shan_nan.mp3"));
		remoteMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		remoteMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.start();
			}
		});
		try {
			if(null != remoteMediaPlayer) {
				remoteMediaPlayer.stop();
			}
			remoteMediaPlayer.prepareAsync();
			
			// 不需要的时候，可以释放资源
			// when we don’t need the instance of MediaPlayer anymore, we should release it
			// remoteMediaPlayer.release(); 
		} catch (IllegalStateException e) {
			Toast.makeText(this, "加载远程音乐文件异常："+ e.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.local_prepare_button:
			initLocalMediaPlayer();
			break;
		case R.id.local_play_button:
			localMediaPlayer.start();
			break;
		case R.id.remote_prepare_button:
			initRemoteMediaPlayer();
			break;
		case R.id.remote_play_button:
			remoteMediaPlayer.start();
			break;
		case R.id.take_photo_button:
			takePhoto();
			break;
		default:
			break;
		}
	}
	
	private void takePhoto() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, CAPTURE_REQUEST_CODE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			
		if(requestCode == CAPTURE_REQUEST_CODE) {
			Bitmap bitmap = (Bitmap)data.getExtras().get("data");
			takedPhotoIv.setImageBitmap(bitmap);
		}
	}
}
